#include "frmPaging.h"
#include "PagingSystem.h"

/*
#include "GameSystemManager.h"
#include "GameSceneManager.h"
#include "GameObjectManager.h"
#include "CVelocity.h"
*/

frmPaging::frmPaging() {
	subscribeEvents();
	setup();
}
frmPaging::~frmPaging() {

}

void frmPaging::populateTreePageInfo(std::string name) {

	if(PagingConfig.selectedPagedTrees == "")
		return;

	PagedObjects *po = GameSystemManager::getSingleton()->getPaging()->getFoliageByName(name);
	
	std::string res;
	CEGUI::Editbox* txtTmp;
	
	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesName");
	txtTmp->setText(name);

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesPageSize");
	txtTmp->setText(StringConverter::toString(po->pageSize));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesBatchDistance");
	txtTmp->setText(StringConverter::toString(po->batchDistance));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesBatchFadeDistance");
	txtTmp->setText(StringConverter::toString(po->batchFade));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesImpostorDistance");
	txtTmp->setText(StringConverter::toString(po->impostorDist));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesImpostorFadeDistance");
	txtTmp->setText(StringConverter::toString(po->impostorFade));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesQuantity");
	txtTmp->setText(StringConverter::toString(po->quantity));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesMinSize");
	txtTmp->setText(StringConverter::toString(po->minScale));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesMaxSize");
	txtTmp->setText(StringConverter::toString(po->maxScale));

	//std::string name,Real pageSize,Real batchDistance, Real batchFade, Real impostorDist, Real impostorFade, std::string meshName, unsigned int quantity, Real minScale, Real maxScale) {
	//GameSystemManager::getSingleton()->getPaging()->addTreePage(objectName,pageSize,batchDist,batchFade,impDist,impFadeDist,PagingConfig.selectedTreeMesh,quantity,minScale,maxScale);
	
	//populatePagedTreesList();
	
}

void frmPaging::populateGrassPageInfo(std::string name) {
	
	if(PagingConfig.selectedPagedGrass == "")
		return;

	PagedObjects *po = GameSystemManager::getSingleton()->getPaging()->getFoliageByName(name);

	CEGUI::Editbox* txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassName");
	txtTmp->setText(name);

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassPageSize");
	txtTmp->setText(StringConverter::toString(po->pageSize));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesBatchDistance");
	txtTmp->setText(StringConverter::toString(po->batchDistance));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassMinSize");
	txtTmp->setText(StringConverter::toString(po->minScale));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassMaxSize");
	txtTmp->setText(StringConverter::toString(po->maxScale));

	CEGUI::Checkbox* chkAnimated = (CEGUI::Checkbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/chkGrassSway");
	if(po->animated)
		chkAnimated->setSelected(true);
	else
		chkAnimated->setSelected(false);

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassSwayLenght");
	txtTmp->setText(StringConverter::toString(po->swayLenght));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassSwaySpeed");
	txtTmp->setText(StringConverter::toString(po->swaySpeed));

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassDensity");
	txtTmp->setText(StringConverter::toString(po->density));

	if(po->ft == FADETECH_ALPHA) {
		GameSystemManager::getSingleton()->getGUI()->selectItemCombobox("Paging/cmbGrassFadeTechnique", "FADETECH_ALPHA");
	} else if (po->ft == FADETECH_ALPHAGROW) {
		GameSystemManager::getSingleton()->getGUI()->selectItemCombobox("Paging/cmbGrassFadeTechnique", "FADETECH_ALPHAGROW");
	} else if(po->ft == FADETECH_GROW) {
		GameSystemManager::getSingleton()->getGUI()->selectItemCombobox("Paging/cmbGrassFadeTechnique", "FADETECH_GROW");
	}

	if(po->grt == GRASSTECH_CROSSQUADS) {
		GameSystemManager::getSingleton()->getGUI()->selectItemCombobox("Paging/cmbGrassRenderTechnique", "GRASSTECH_CROSSQUADS");
	} else if (po->grt == GRASSTECH_QUAD) {
		GameSystemManager::getSingleton()->getGUI()->selectItemCombobox("Paging/cmbGrassRenderTechnique", "GRASSTECH_QUAD");
	} else if(po->grt == GRASSTECH_SPRITE) {
		GameSystemManager::getSingleton()->getGUI()->selectItemCombobox("Paging/cmbGrassRenderTechnique", "GRASSTECH_SPRITE");
	}
	
	
}



void frmPaging::populateGrassList() {
	CEGUI::Listbox* lbox = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/lstGrass");
	lbox->resetList();
	
	Ogre::MaterialManager::ResourceMapIterator mat = Ogre::MaterialManager::getSingleton().getResourceIterator();
    while(mat.hasMoreElements())
    { 
		Ogre::MaterialPtr mat2 = static_cast<Ogre::MaterialPtr>(mat.getNext());
		if(mat2->getGroup() == "PagedGeometry") {
			std::string matName = mat2->getName();
			if(matName.substr(0,6) == "grass_") {
				CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem(matName);
				lbox->addItem(item);
			}
		}
	}
}

void frmPaging::populateTreeList() {
	CEGUI::Listbox* lbox = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/lstTrees");
	lbox->resetList();
	StringVectorPtr resourceNames = ResourceGroupManager::getSingleton().findResourceNames("PagedGeometry","Trees/*.mesh",false );
	std::vector<Ogre::String>::const_iterator itResourceName = resourceNames->begin();
	while ( itResourceName != resourceNames->end() )
	{
		std::string meshName = itResourceName->substr(6,itResourceName->length() - 6);
		CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem(meshName);
		lbox->addItem(item);
		++itResourceName;
	} 
}

void frmPaging::populatePagedGrassList() {
	CEGUI::Listbox* lbox = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/lstPagedGrass");
	lbox->resetList();
	
	std::map<std::string, PagedObjects*> foliage;
	foliage = GameSystemManager::getSingleton()->getPaging()->getFoliage();

	std::map<std::string, PagedObjects*>::iterator iter;
	for(iter = foliage.begin(); iter != foliage.end(); iter++) {
		if(iter->second->isGrass) {
			CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem(iter->first);
			lbox->addItem(item);
		}
	}	
}

void frmPaging::populatePagedTreesList() {
	CEGUI::Listbox* lbox = (CEGUI::Listbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/lstPagedTrees");
	lbox->resetList();
	
	std::map<std::string, PagedObjects*> foliage;
	foliage = GameSystemManager::getSingleton()->getPaging()->getFoliage();

	std::map<std::string, PagedObjects*>::iterator iter;
	for(iter = foliage.begin(); iter != foliage.end(); iter++) {
		if(iter->second->isTree) {
			CEGUI::ListboxTextItem* item = new CEGUI::ListboxTextItem(iter->first);
			lbox->addItem(item);
		}
	}	
}
void frmPaging::addGrass() {

	std::string objectName;
	Real pageSize;
	Real batchDistance;
	Real minScale;
	Real maxScale;

	bool animated;
	Real swayLenght;
	Real swaySpeed;
	Real density;
	GrassTechnique grt;
	FadeTechnique ft;

	std::string res;
	CEGUI::Editbox* txtTmp;

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassName");
	objectName = txtTmp->getText().c_str();
	if(objectName == "")
		return;

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassPageSize");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		pageSize = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesBatchDistance");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		batchDistance = StringConverter::parseReal(txtTmp->getText().c_str());

	if(PagingConfig.selectedGrassImg == "")
		return;

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassMinSize");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		minScale = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassMaxSize");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		maxScale = StringConverter::parseReal(txtTmp->getText().c_str());

	CEGUI::Checkbox* chkAnimated = (CEGUI::Checkbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/chkGrassSway");
	if(chkAnimated->isSelected())
		animated = true;
	else
		animated = false;

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassSwayLenght");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		swayLenght = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassSwaySpeed");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		swaySpeed = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtGrassDensity");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		density = StringConverter::parseReal(txtTmp->getText().c_str());

	CEGUI::Combobox* cmbFadeTech = (CEGUI::Combobox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/cmbGrassFadeTechnique");
	std::string fadeTech = (std::string)cmbFadeTech->getText().c_str();
	
	if(fadeTech == "FADETECH_ALPHA") {
		ft = FADETECH_ALPHA;
	} else if (fadeTech == "FADETECH_ALPHAGROW") {
		ft = FADETECH_ALPHAGROW;
	} else if(fadeTech == "FADETECH_GROW") {
		ft = FADETECH_GROW;
	}

	CEGUI::Combobox* cmbRenderTech = (CEGUI::Combobox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/cmbGrassRenderTechnique");

	std::string grassTech = (std::string)cmbRenderTech->getText().c_str();
	
	if(grassTech == "GRASSTECH_CROSSQUADS") {
		grt = GRASSTECH_CROSSQUADS;
	} else if (grassTech == "GRASSTECH_QUAD") {
		grt = GRASSTECH_QUAD;
	} else if(grassTech == "GRASSTECH_SPRITE") {
		grt = GRASSTECH_SPRITE;
	}

	GameSystemManager::getSingleton()->getPaging()->addGrassPage(objectName,pageSize,batchDistance,PagingConfig.selectedGrassImg,minScale,maxScale,animated,swayLenght,swaySpeed,density,grt,ft);

	populatePagedGrassList();
}
void frmPaging::addTrees() {

	std::string objectName;
	Real pageSize;
	Real batchDist;
	Real batchFade;
	Real impDist;
	Real impFadeDist;
	unsigned int quantity;
	Real minScale;
	Real maxScale;

	std::string res;
	CEGUI::Editbox* txtTmp;

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesName");
	objectName = txtTmp->getText().c_str();
	if(objectName == "")
		return;
	
	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesPageSize");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		pageSize = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesBatchDistance");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		batchDist = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesBatchFadeDistance");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		batchFade = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesImpostorDistance");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		impDist = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesImpostorFadeDistance");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		impFadeDist = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesQuantity");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		quantity = StringConverter::parseReal(txtTmp->getText().c_str());
	
	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesMinSize");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		minScale = StringConverter::parseReal(txtTmp->getText().c_str());

	txtTmp = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/txtTreesMaxSize");
	res = txtTmp->getText().c_str();
	if(res == "")
		return;
	else
		maxScale = StringConverter::parseReal(txtTmp->getText().c_str());

	if(PagingConfig.selectedTreeMesh == "")
		return;

	//std::string name,Real pageSize,Real batchDistance, Real batchFade, Real impostorDist, Real impostorFade, std::string meshName, unsigned int quantity, Real minScale, Real maxScale) {
	GameSystemManager::getSingleton()->getPaging()->addTreePage(objectName,pageSize,batchDist,batchFade,impDist,impFadeDist,PagingConfig.selectedTreeMesh,quantity,minScale,maxScale);
	
	populatePagedTreesList();
}

void frmPaging::subscribeEvents() {
	////////////////////////////////////////////////////////////////////////////////////////////////////

	CEGUI::Window* wndPagingClose = CEGUI::WindowManager::getSingleton().getWindow("Paging");
	wndPagingClose->subscribeEvent(CEGUI::FrameWindow::EventCloseClicked,CEGUI::Event::Subscriber(&frmPaging::evtPagingClose_click,this));

	CEGUI::Window* lstTreesMesh = CEGUI::WindowManager::getSingleton().getWindow("Paging/lstTrees");
	lstTreesMesh->subscribeEvent(CEGUI::Listbox::EventSelectionChanged,CEGUI::Event::Subscriber(&frmPaging::evtPaginglstTrees_click,this));

	CEGUI::Window* lstTreesPaged = CEGUI::WindowManager::getSingleton().getWindow("Paging/lstPagedTrees");
	lstTreesPaged->subscribeEvent(CEGUI::Listbox::EventSelectionChanged,CEGUI::Event::Subscriber(&frmPaging::evtPaginglstPagedTrees_click,this));

	CEGUI::Window* lstGrassImg = CEGUI::WindowManager::getSingleton().getWindow("Paging/lstGrass");
	lstGrassImg->subscribeEvent(CEGUI::Listbox::EventSelectionChanged,CEGUI::Event::Subscriber(&frmPaging::evtPaginglstGrass_click,this));

	CEGUI::Window* lstGrassPaged = CEGUI::WindowManager::getSingleton().getWindow("Paging/lstPagedGrass");
	lstGrassPaged->subscribeEvent(CEGUI::Listbox::EventSelectionChanged,CEGUI::Event::Subscriber(&frmPaging::evtPaginglstPagedGrass_click,this));

	CEGUI::Window* btnAddTree = CEGUI::WindowManager::getSingleton().getWindow("Paging/btnAddTree");
	btnAddTree->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPaging::evtPagingAddTree_click,this));
	
	CEGUI::Window* btnRemovePagedTree = CEGUI::WindowManager::getSingleton().getWindow("Paging/btnRemovePagedTrees");
	btnRemovePagedTree->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPaging::evtPagingRemoveTrees_click,this));
	
	CEGUI::Window* btnRndTrees = CEGUI::WindowManager::getSingleton().getWindow("Paging/btnRandomizeTrees");
	btnRndTrees->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPaging::evtPagingRandomizeTrees_click,this));

	CEGUI::Window* btnAddGrass = CEGUI::WindowManager::getSingleton().getWindow("Paging/btnAddGrass");
	btnAddGrass->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPaging::evtPagingAddGrass_click,this));
	
	CEGUI::Window* btnRemovePagedGrass = CEGUI::WindowManager::getSingleton().getWindow("Paging/btnRemovePagedGrass");
	btnRemovePagedGrass->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPaging::evtPagingRemoveGrass_click,this));
	
	CEGUI::Window* btnRndGrass = CEGUI::WindowManager::getSingleton().getWindow("Paging/btnRandomizeGrass");
	btnRndGrass->subscribeEvent(CEGUI::PushButton::EventClicked,CEGUI::Event::Subscriber(&frmPaging::evtPagingRandomizeGrass_click,this));

	CEGUI::Combobox* cmbGrassFadeTech = (CEGUI::Combobox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/cmbGrassFadeTechnique");
	cmbGrassFadeTech->subscribeEvent(CEGUI::Combobox::EventListSelectionAccepted,CEGUI::Event::Subscriber(&frmPaging::evtPagingGrassFadeTechnique_change,this));
	
	CEGUI::Combobox* cmbGrassRenderTech = (CEGUI::Combobox*) CEGUI::WindowManager::getSingleton().getWindow("Paging/cmbGrassRenderTechnique");
	cmbGrassRenderTech->subscribeEvent(CEGUI::Combobox::EventListSelectionAccepted,CEGUI::Event::Subscriber(&frmPaging::evtPagingGrassRenderTechnique_change,this));

	CEGUI::Window* sldBrushSize = CEGUI::WindowManager::getSingleton().getWindow("Paging/sldBrushSize");
	sldBrushSize->subscribeEvent(CEGUI::Slider::EventValueChanged,CEGUI::Event::Subscriber(&frmPaging::evtPagingBrushSize_change,this));

	////////////////////////////////////////////////////////////////////////////////////////////////////////

	//CEGUI::Checkbox* chk = (CEGUI::Checkbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/chkIsRagdoll");
	//chk->subscribeEvent(CEGUI::RadioButton::EventSelectStateChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightIsRagdoll_change,this));

	//CEGUI::Editbox* txtLightType = (CEGUI::Editbox*) CEGUI::WindowManager::getSingleton().getWindow("Objects/txtLightRange");
	//txtLightType->subscribeEvent(CEGUI::Editbox::EventTextChanged,CEGUI::Event::Subscriber(&frmObjects::evtObjectsLightRange_change,this));

}

void frmPaging::setup() {
	CEGUI::Window* w = CEGUI::WindowManager::getSingleton().getWindow("Paging");
	w->setVisible(true);

	populateTreeList();
	populateGrassList();

	CEGUI::Combobox* cmb = (CEGUI::Combobox*)CEGUI::WindowManager::getSingleton().getWindow("Paging/cmbGrassFadeTechnique");
	cmb->setReadOnly(true);

	CEGUI::Slider* sld = (CEGUI::Slider*) CEGUI::WindowManager::getSingleton().getWindow("Paging/sldBrushSize");
	sld->setMaxValue(530);
	sld->setCurrentValue(1.0f);

	const CEGUI::Image* sel_img = &CEGUI::ImagesetManager::getSingleton().getImageset("TaharezLook")->getImage("MultiListSelectionBrush");
	CEGUI::ListboxTextItem* itm1;
	itm1 = new CEGUI::ListboxTextItem("FADETECH_GROW", 1);
    itm1->setSelectionBrushImage(sel_img);
    cmb->addItem(itm1);

	CEGUI::ListboxTextItem* itm2;
	itm2 = new CEGUI::ListboxTextItem("FADETECH_ALPHA", 2);
    itm2->setSelectionBrushImage(sel_img);
    cmb->addItem(itm2);

	CEGUI::ListboxTextItem* itm3;
	itm3 = new CEGUI::ListboxTextItem("FADETECH_ALPHAGROW", 2);
    itm3->setSelectionBrushImage(sel_img);
    cmb->addItem(itm3);

	cmb = (CEGUI::Combobox*)CEGUI::WindowManager::getSingleton().getWindow("Paging/cmbGrassRenderTechnique");
	cmb->setReadOnly(true);

	CEGUI::ListboxTextItem* itm4;
	itm4 = new CEGUI::ListboxTextItem("GRASSTECH_SPRITE", 2);
    itm4->setSelectionBrushImage(sel_img);
    cmb->addItem(itm4);

	CEGUI::ListboxTextItem* itm5;
	itm5 = new CEGUI::ListboxTextItem("GRASSTECH_QUAD", 2);
    itm5->setSelectionBrushImage(sel_img);
    cmb->addItem(itm5);

	CEGUI::ListboxTextItem* itm6;
	itm6 = new CEGUI::ListboxTextItem("GRASSTECH_CROSSQUADS", 2);
    itm6->setSelectionBrushImage(sel_img);
    cmb->addItem(itm6);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//MenuBar GUI Events
bool frmPaging::evtPagingClose_click(const CEGUI::EventArgs& e) {
	CEGUI::Window* w = CEGUI::WindowManager::getSingleton().getWindow("Paging");
	w->setVisible(false);
	return true;
}

bool frmPaging::evtPaginglstTrees_click(const CEGUI::EventArgs& e) {
	const CEGUI::WindowEventArgs& wEventArgs = static_cast<const CEGUI::WindowEventArgs&>(e);
    CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(wEventArgs.window);

    const CEGUI::String Name = list->getName ();
    size_t selCount = list->getSelectedCount ();
    if (selCount == 1)
    {
		CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem ();
		const unsigned int pos = StringConverter::parseUnsignedInt (item->getText().c_str ());   

		item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

		const char *treeMeshName = item->getText().c_str();
		item->setSelected(true);
		PagingConfig.selectedTreeMesh = treeMeshName;
		
	}

	return true;
}
bool frmPaging::evtPaginglstPagedTrees_click(const CEGUI::EventArgs& e) {
	const CEGUI::WindowEventArgs& wEventArgs = static_cast<const CEGUI::WindowEventArgs&>(e);
    CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(wEventArgs.window);

    const CEGUI::String Name = list->getName ();
    size_t selCount = list->getSelectedCount ();
    if (selCount == 1)
    {
		CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem ();
		const unsigned int pos = StringConverter::parseUnsignedInt (item->getText().c_str ());   

		item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

		const char *treePagedName = item->getText().c_str();
		item->setSelected(true);
		
		PagingConfig.selectedPagedTrees = treePagedName;
		populateTreePageInfo(PagingConfig.selectedPagedTrees);
	}
	return true;
}

bool frmPaging::evtPagingAddTree_click(const CEGUI::EventArgs& e) {
	GameSystemManager::getSingleton()->getGUI()->showLoader(true);
	addTrees();
	GameSystemManager::getSingleton()->getGUI()->showLoader(false);
	return true;
}
bool frmPaging::evtPagingRandomizeTrees_click(const CEGUI::EventArgs& e) {
	GameSystemManager::getSingleton()->getGUI()->showLoader(true);
	GameSystemManager::getSingleton()->getPaging()->randomizeTreePage(PagingConfig.selectedPagedTrees);
	GameSystemManager::getSingleton()->getGUI()->showLoader(false);
	return true;
}
bool frmPaging::evtPagingRemoveTrees_click(const CEGUI::EventArgs& e) {
	if(PagingConfig.selectedPagedTrees != "") {
		GameSystemManager::getSingleton()->getPaging()->removePagedObject(PagingConfig.selectedPagedTrees);
		populatePagedTreesList();
	}
	return true;
}

bool frmPaging::evtPaginglstGrass_click(const CEGUI::EventArgs& e) {
	const CEGUI::WindowEventArgs& wEventArgs = static_cast<const CEGUI::WindowEventArgs&>(e);
    CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(wEventArgs.window);

    const CEGUI::String Name = list->getName ();
    size_t selCount = list->getSelectedCount ();
    if (selCount == 1)
    {
		CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem ();
		const unsigned int pos = StringConverter::parseUnsignedInt (item->getText().c_str ());   

		item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

		const char *grassName = item->getText().c_str();
		item->setSelected(true);
		
		PagingConfig.selectedGrassImg = grassName;
	}
	return true;
}
bool frmPaging::evtPaginglstPagedGrass_click(const CEGUI::EventArgs& e) {
	const CEGUI::WindowEventArgs& wEventArgs = static_cast<const CEGUI::WindowEventArgs&>(e);
    CEGUI::Listbox* list = static_cast<CEGUI::Listbox*>(wEventArgs.window);

    const CEGUI::String Name = list->getName ();
    size_t selCount = list->getSelectedCount ();
    if (selCount == 1)
    {
		CEGUI::ListboxTextItem* item =  (CEGUI::ListboxTextItem*) list->getFirstSelectedItem ();
		const unsigned int pos = StringConverter::parseUnsignedInt (item->getText().c_str ());   

		item->setSelectionBrushImage("TaharezLook", "MultiListSelectionBrush");

		const char *grassPagedName = item->getText().c_str();
		item->setSelected(true);
		
		PagingConfig.selectedPagedGrass = grassPagedName;
		populateGrassPageInfo(PagingConfig.selectedPagedGrass);
	}
	return true;
}

bool frmPaging::evtPagingAddGrass_click(const CEGUI::EventArgs& e) {
	GameSystemManager::getSingleton()->getGUI()->showLoader(true);
	addGrass();
	GameSystemManager::getSingleton()->getGUI()->showLoader(false);
	return true;
}

bool frmPaging::evtPagingRandomizeGrass_click(const CEGUI::EventArgs& e) {
	GameSystemManager::getSingleton()->getGUI()->showLoader(true);

	GameSystemManager::getSingleton()->getGUI()->showLoader(false);
	return true;
}

bool frmPaging::evtPagingRemoveGrass_click(const CEGUI::EventArgs& e) {
	if(PagingConfig.selectedPagedGrass != "") {
		GameSystemManager::getSingleton()->getPaging()->removePagedObject(PagingConfig.selectedPagedGrass);
		populatePagedGrassList();
	}
	return true;
}

bool frmPaging::evtPagingGrassFadeTechnique_change(const CEGUI::EventArgs& e) {

	return true;
}

bool frmPaging::evtPagingGrassRenderTechnique_change(const CEGUI::EventArgs& e) {

	return true;
}

bool frmPaging::evtPagingBrushSize_change(const CEGUI::EventArgs& e) {
	CEGUI::Slider *sl = (CEGUI::Slider*)CEGUI::WindowManager::getSingleton().getWindow("Paging/sldBrushSize");
	GameSystemManager::getSingleton()->getPaging()->setBrushSize(sl->getCurrentValue());
	return true;
}
